<?php
/*
Plugin Name: Megatop
Plugin URI: http://vmrf.ru
Description: Show mega top news
Author: vmrf.ru
Version: 1.0
Author URI: http://vmrf.ru
 */

namespace AY\Widget;

class RegionTop extends \AY\Widget\AWidget
{
    protected $widgetName = 'RegionTop';
    protected $widgetOptions = array(
        'classname' => 'region-top-ay',
        'description'   => 'widget show region top news'
        );
    protected $countPost = 10;
    protected $category_id;
    protected $arch_url;

    
    protected function getData() {
        $this->category_id = ay()->region()->getCategoryByRegion();
        $slug = ay()->region()->getRegion();
        
        $args = array('numberposts' => $this->countPost);
        if($this->category_id){
            $args['category'] = $this->category_id;
            $this->arch_url = "/category/" .$slug;
        } else {
            $args['offset'] = $this->countPost;
            $this->arch_url = date('/Y/m/');
        }

        return get_posts( $args );
    }


    function widget( $args, $instance ) {
        $resentPost = $this->getData();
        ?>
        <div class="column_title clearfix">
            <h3><a href="<?php echo $this->arch_url; ?>">Новости региона</a></h3>
            <a href=""><img src="<?php echo get_template_directory_uri(); ?>/assets/images/rss.png" alt="" /></a>
         </div>
        <div style="clear:both"></div>
        <div class="news_list">
        <?php foreach ($resentPost as $post) :setup_postdata($post); ?>
            <div class="item clearfix">
                <a href="<?php echo get_permalink($post->ID); ?>"
                   title="<?php echo htmlspecialchars($post->post_title); ?>">
                <?php
                if ( has_post_thumbnail($post->ID) ) { // check if the post has a Post Thumbnail assigned to it.
                    echo get_the_post_thumbnail($post->ID, array(75,75));
                } else {
                    echo '<img src="'. get_template_directory_uri().'/assets/images/vm_tumb.jpg" alt="" />';
                }?>
                </a>
                <a href="<?php echo get_permalink($post->ID); ?>"
                   title="<?php echo htmlspecialchars($post->post_title); ?>"><?php echo $post->post_title ?></a><br>
                <?php echo $post->post_excerpt?><a href="<?php echo get_permalink($post->ID); ?>"> Дальше &rarr;</a>
                 <div class="tools">
                    <span class="date"><span><?php echo date("H:i ",strtotime($post->post_date)); ?></span><?php echo date("d/m",strtotime($post->post_date)); ?></span>
                <?php
                    $post_categories = wp_get_post_categories($post->ID);
                    $cats = array();
                    foreach($post_categories as $c){
                        $cat = get_category( $c );
                        echo "<a href='/category/".$cat->slug."' class='category'>".$cat->name." </a>";
                    }
                ?>
                </div>
            </div>
        <?php endforeach; ?>
        <div style="padding-bottom: 10px;" >
            <a href="<?php echo $this->arch_url; ?>">Остальные события &rarr;</a>
        </div>
        </div><!-- .new_list -->
        <?php
    }
    
    function form( $instance ) {
    }
    
    function update($new_instance, $old_instance) {
    }
    
}
